﻿using Ozone.Help;
using Ozone.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.DAL
{
    public class MethodDAL
    {

        private static List<execution_method> _allexecution_method;

        /// <summary>
        /// 查询方法列表
        /// </summary>
        /// <returns></returns>
        public static List<execution_method> GetALLMethodList()
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                //根据ID查询
                _allexecution_method = db.Queryable<execution_method>()
                .OrderBy(p => p.CreateTime, OrderByType.Desc)
                .ToList();
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询方法列表发生异常", ex);
                _allexecution_method = null;
            }
            return _allexecution_method;
        }


        public static List<execution_method> GetALLMethod(string MethodName)
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                //根据ID查询
                _allexecution_method = db.Queryable<execution_method>()
                .Where(p => p.MethodName == MethodName)
                .OrderBy(p => p.CreateTime, OrderByType.Desc)
                .ToList();
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询方法列表发生异常", ex);
                _allexecution_method = null;
            }
            return _allexecution_method;
        }


        /// <summary>
        /// 添加方法信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool AddMethod(execution_method Model)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (Model != null)
                {
                    IsSuccess = db.Insertable<execution_method>(Model).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加方法异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }


        /// <summary>
        /// 修改序列信息
        /// </summary>
        /// <param name="FID"></param>
        /// <param name="MethodConten"></param>
        /// <returns></returns>
        public static bool UpdateMethodExecSerial(string FID, string MethodConten)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(FID) && !string.IsNullOrEmpty(MethodConten))
                {
                    IsSuccess = db.Updateable<execution_method>()
                    .SetColumns(p => p.MethodConten == MethodConten)
                    .SetColumns(p => p.ModifyTime == DateTime.Now)
                    .Where(p => p.FID == FID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新方法序列异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }



        /// <summary>
        /// 删除方法信息
        /// </summary>
        /// <param name="FID"></param>
        /// <returns></returns>
        public static bool DeleteMethod(string FID)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(FID))
                {
                    IsSuccess = db.Deleteable<execution_method>().Where(p => p.FID == FID).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("删除方法异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }

        /// <summary>
        /// 修改方法
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool UpdateMethod(execution_method Model)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (Model != null)
                {
                    IsSuccess = db.Updateable<execution_method>()
                    .SetColumns(p => p.MethodName == Model.MethodName)
                    .SetColumns(p => p.MethodState == Model.MethodState)
                    .SetColumns(p => p.ModifyTime == Model.ModifyTime)
                    .Where(p => p.FID == Model.FID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("修改日志异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }




        /// <summary>
        /// 修改方法执行序列ID
        /// </summary>
        /// <param name="FID"></param>
        /// <param name="ExecSerialNumber"></param>
        /// <returns></returns>
        public static bool UpdateMethodExecSerialNumber(string FID, string ExecSerialNumber)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(FID) && !string.IsNullOrEmpty(ExecSerialNumber))
                {
                    IsSuccess = db.Updateable<execution_method>()
                    .SetColumns(p => p.ExecSerialNumber == Convert.ToInt32(ExecSerialNumber))
                    .SetColumns(p => p.ModifyTime == DateTime.Now)
                    .Where(p => p.FID == FID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("修改执行方法序列异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }



        /// <summary>
        /// 修改方法执行序列步骤编号
        /// </summary>
        /// <param name="FID"></param>
        /// <param name="ExecSerialStepNumber"></param>
        /// <returns></returns>
        public static bool UpdateMethodExecSerialStepNumber(string FID, string ExecSerialStepNumber)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(FID) && !string.IsNullOrEmpty(ExecSerialStepNumber))
                {
                    IsSuccess = db.Updateable<execution_method>()
                    .SetColumns(p => p.ExecSerialStepNumber == Convert.ToInt32(ExecSerialStepNumber))
                    .SetColumns(p => p.ModifyTime == DateTime.Now)
                    .Where(p => p.FID == FID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("修改执行方法序列步骤异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }
    }
}
